﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BlockeyEssentials.Input
{
    public class GamePadInput
    {
        public GamePadInput(PlayerIndex oPlayerIndex)
        {
            _PlayerIndex = oPlayerIndex;
            _PrevState = GamePad.GetState(_PlayerIndex);
            _CurrState = GamePad.GetState(_PlayerIndex);
        }

        #region Methods

        /// <summary>
        /// Updates the current and previous states.
        /// Must be called every frame to work correctly.
        /// </summary>
        public void Update()
        {
            _PrevState = _CurrState;
            _CurrState = GamePad.GetState(_PlayerIndex);
        }

        /// <summary>
        /// Determines if a button was just pressed.
        /// </summary>
        /// <param name="oButtons">Button to check</param>
        /// <returns>True if button was just pressed</returns>
        public bool JustPressed(params Buttons[] oButtons)
        {
            foreach (Buttons oButton in oButtons)
            {
                bool buttonUp = _PrevState.IsButtonUp(oButton);
                bool buttonDown = _CurrState.IsButtonDown(oButton);

                if (buttonUp && buttonDown)
                    return true;
            }

            return false;
        }


        /// <summary>
        /// Determines if a button was just released.
        /// </summary>
        /// <param name="oButton">Button to check</param>
        /// <returns>True of button was just released</returns>
        public bool JustReleased(Buttons oButton)
        {
            bool keyDown = _PrevState.IsButtonDown(oButton);
            bool keyUp = _CurrState.IsButtonUp(oButton);

            if ((keyDown && keyUp) == false)
                return false;

            return true;
        }

        /// <summary>
        /// Determines if the button or buttons are currently being pressed.
        /// </summary>
        /// <param name="oButtons">Button(s) to check</param>
        /// <returns>True if all the buttons are being pressed</returns>
        public bool IsPressed(params Buttons[] oButtons)
        {
            foreach (Buttons button in oButtons)
            {
                bool keyDown = _CurrState.IsButtonDown(button);

                if (keyDown == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Determines if the button or buttons are currently being released.
        /// </summary>
        /// <param name="oButtons">Button(s) to check</param>
        /// <returns>True if all the buttons are being released</returns>
        public bool IsReleased(params Buttons[] oButtons)
        {
            foreach (Buttons button in oButtons)
            {
                bool keyUp = _CurrState.IsButtonUp(button);

                if (keyUp == false)
                    return false;
            }

            return true;
        }

        #endregion

        public Vector2 LeftStick
        {
            get { return _CurrState.ThumbSticks.Left; }
        }

        public Vector2 RightStick
        {
            get { return _CurrState.ThumbSticks.Right; }
        }

        public float LeftTrigger
        {
            get { return _CurrState.Triggers.Left; }
        }

        public float RightTrigger
        {
            get { return _CurrState.Triggers.Right; }
        }

        GamePadState _PrevState;
        GamePadState _CurrState;
        PlayerIndex _PlayerIndex;

    }
}
